package com.syz.web.service.Impl;

import com.syz.web.dao.IUserDao;
import com.syz.web.dao.Impl.UserDaoImpl;
import com.syz.web.pojo.User;
import com.syz.web.pojo.query.UserQuery;
import com.syz.web.service.IUserService;
import com.syz.web.util.LayUITableResult;
import com.syz.web.util.PageInfo;

import java.util.List;

public class UserServiceImpl implements IUserService {
    private IUserDao userDao = new UserDaoImpl();

    //业务：为了封装PageInfo，查询两次数据库
//    @Override
//    public PageInfo<User> selectByPage(Integer pageNo, Integer pageSize) {
//        List<User> list = userDao.selectByPage(pageNo, pageSize);
//        Integer totalCount = userDao.selectTotalCount();
//        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
//        PageInfo<User> pageInfo = new PageInfo<>(list, totalPage, pageNo, pageSize);
//        return pageInfo;
//    }
    @Override
    public LayUITableResult selectByPage(UserQuery userQuery) {
        List<User> list = userDao.selectByPage(userQuery);
        Integer totalCount = userDao.selectTotalCount(userQuery);
        //int totalPage = (int)Math.ceil((double)totalCount / pageSize);

        return LayUITableResult.ok(totalCount, list);
    }
    @Override
    public boolean deleteById(int id) {
        int count = userDao.deleteById(id);
        return count == 1 ? true : false;
    }

    @Override
    public boolean add(User user) {
        int count = userDao.add(user);
        return count == 1 ? true : false;
    }

    @Override
    public User selectById(int id) {
        return userDao.selectById(id);
    }

    @Override
    public boolean update(User user) {
        int count = userDao.update(user);
        return count == 1 ? true : false;
    }

    @Override
    public User login(String name, String password) {
        return userDao.login(name, password);

       /* if (user != null) {

            return true;
        } else {
            return false;
        }*/
        //return user != null ? true : false;
    }

    @Override
    public boolean deleteAll(String[] ids) {
        // delete from user where id in();
        int count = 0;
        for (String id : ids) {
            count += userDao.deleteById(Integer.parseInt(id));
        }
        return count == ids.length ? true : false;
    }
}
